﻿    /*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.CSS
{
    #region Using Statements

    using System;
    using KLF.Lib.Browser.DOM.Style;
    using System.Text.RegularExpressions;

    #endregion

    /// <summary>
    /// The CSSImportRule interface represents a @import rule within a CSS 
    /// style sheet. The @import rule is used to import style rules from 
    /// other style sheets.
    /// </summary>
    public class CSSImportRule : CSSRule
    {
        #region Constructors

        internal CSSImportRule()
        {
            href = "";
            media = new MediaList();
        }

        internal CSSImportRule(string uri)
        {
            href = uri;
        }

        #endregion

        #region Methods

        internal void AddMedia(string m)
        {
            media.List.Add(m);
        }

        #endregion

        #region DOM Level 2 CSS Compliance

        /// <summary>
        /// The location of the style sheet to be imported. The attribute 
        /// will not contain the "url(...)" specifier around the URI.
        /// </summary>
        public string href
        {
            get
            {
                return _href != null ? _href.Value : "";
            }
            internal set
            {
                Regex regex = new Regex(@"url[\(\""](?<uri>[^""\)]+)");

                if (regex.IsMatch(value))
                {
                    Match m = regex.Match(value);
                    _href = new StringLookup(m.Groups["uri"].Value);
                }
                else
                    _href = new StringLookup(value);
            }
        }
        internal StringLookup _href;

        /// <summary>
        /// A list of media types for which this style sheet may be used.
        /// </summary>
        public MediaList media
        {
            get;
            internal set;
        }

        /// <summary>
        /// The style sheet referred to by this rule, if it has been loaded. 
        /// The value of this attribute is null if the style sheet has not 
        /// yet been loaded or if it will not be loaded (e.g. if the style
        /// sheet is for a media type not supported by the user agent).
        /// </summary>
        public CSSStyleSheet styleSheet
        {
            get;
            internal set;
        }

        #endregion
    }
}
